import {initializationDate, getModifyTime, go} from '../../../utils/util';

/**
 * @author 老刘
 * @desc 自定义日期选择
 */
const app = getApp();
Page({
	data: {
		currentDate: app.globalData.choiceDateData,
		isShowPre: true,// 是否显示前一天|月
		isShowNext: false// 是否显示后一天|月
	},
	onLoad () {
		// 计算当前月到2018-8月日历,push到数组
		app.globalData.startDateYear = 2018;
		app.globalData.startDateMonth = 8;
	},
	onShow () {
		if (wx.getStorageSync('isNotRefresh') && wx.getStorageSync('isNotRefresh') === true) {
			let currentDate = wx.getStorageSync('index-choiceData');
			if (currentDate) {
				currentDate = JSON.parse(currentDate);
				this.setData({
					currentDate
				});
				app.globalData.choiceDateData = currentDate;
			}
			wx.removeStorageSync('isNotRefresh');
			return;
		}
		this.setData({
			currentDate: app.globalData.choiceDateData,
			type: this.data.currentDate.type
		});
		// 登录页返回
		let loginInfoFinal = wx.getStorageSync('login_info_final');
		if (loginInfoFinal) {
			this.setData({
				loginInfo: JSON.parse(loginInfoFinal)
			});
			wx.removeStorageSync('login_info_final');
		}
		this.initDate();
	},
	// 初始化顶部时间
	initDate () {
		let {isShowPre, isShowNext} = initializationDate(this.data.currentDate.choiceData, app);
		this.setData({
			isShowPre,
			isShowNext
		});
	},
	changeDate (e) {
		let type;
		if (typeof e === 'number') {
			type = e;
		} else {
			type = e.currentTarget.dataset['type'];
		}
		if (!this.data.isShowNext && type === 1) return;
		if (!this.data.isShowPre && type === -1) return;
		this.setData({
			[`currentDate.choiceData`]: getModifyTime(this.data.currentDate.choiceData, parseInt(type))
		});
		app.globalData.choiceDateData.choiceData = this.data.currentDate.choiceData;
		// 重新初始化
		this.initDate();
	},
	// 选择时间
	onClickChoiceData () {
		go('/pages/characteristic_components/date_picker/date_picker');
	}
});
